<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>替换字符串的子串</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.substr-count.html">substr_count</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.substr.html">substr</a></div>
 <div class="up"><a href="ref.strings.html">字符串函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.substr-replace" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">substr_replace</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">substr_replace</span> &mdash; <span class="dc-title">替换字符串的子串</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.substr-replace-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>substr_replace</strong></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$string</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$replacement</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$start</code></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$length</code></span>
  ] )</div>

  <p class="para rdfs-comment">
   <span class="function"><strong>substr_replace()</strong></span> 在字符串 <em><code class="parameter">string</code></em> 的副本中将由 <em><code class="parameter">start</code></em> 和可选的 <em><code class="parameter">length</code></em> 参数限定的子字符串使用 <em><code class="parameter">replacement</code></em> 进行替换。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.substr-replace-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">string</code></em></dt>

     <dd>

      <p class="para">
       输入字符串。
      </p>
      <p class="para">
       An <span class="type"><a href="language.types.array.html" class="type array">array</a></span> of <span class="type"><a href="language.types.string.html" class="type string">string</a></span>s can be provided, in which
       case the replacements will occur on each string in turn. In this case,
       the <em><code class="parameter">replacement</code></em>, <em><code class="parameter">start</code></em>
       and <em><code class="parameter">length</code></em> parameters may be provided either as
       scalar values to be applied to each input string in turn, or as
       <span class="type"><a href="language.types.array.html" class="type array">array</a></span>s, in which case the corresponding array element will
       be used for each input string.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">replacement</code></em></dt>

     <dd>

      <p class="para">
       替换字符串。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">start</code></em></dt>

     <dd>

      <p class="para">
       如果 <em><code class="parameter">start</code></em> 为正数，替换将从 <em><code class="parameter">string</code></em> 的 <em><code class="parameter">start</code></em> 位置开始。
      </p>
      <p class="para">
       如果 <em><code class="parameter">start</code></em> 为负数，替换将从 <em><code class="parameter">string</code></em> 的倒数第 <em><code class="parameter">start</code></em> 个位置开始。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">length</code></em></dt>

     <dd>

      <p class="para">
       如果设定了这个参数并且为正数，表示 <em><code class="parameter">string</code></em> 中被替换的子字符串的长度。如果设定为负数，它表示待替换的子字符串结尾处距离 <em><code class="parameter">string</code></em> 末端的字符个数。如果没有提供此参数，那么它默认为 strlen( <em><code class="parameter">string</code></em> ) （字符串的长度）。当然，如果 <em><code class="parameter">length</code></em> 为 0，那么这个函数的功能为将 <em><code class="parameter">replacement</code></em> 插入到 <em><code class="parameter">string</code></em> 的 <em><code class="parameter">start</code></em> 位置处。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.substr-replace-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回结果字符串。如果 <em><code class="parameter">string</code></em> 是个数组，那么也将返回一个数组。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.substr-replace-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>4.3.3</td>
       <td>
        All parameters now accept <span class="type"><a href="language.types.array.html" class="type array">array</a></span>s.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.substr-replace-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4816">
    <p><strong>Example #1 <span class="function"><strong>substr_replace()</strong></span> 范例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$var&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'ABCDEFGH:/MNRPQR/'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"Original:&nbsp;</span><span style="color: #0000BB">$var</span><span style="color: #DD0000">&lt;hr&nbsp;/&gt;\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;这两个例子使用&nbsp;“bob”&nbsp;替换整个&nbsp;$var。*/<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bob'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bob'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">))&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;将&nbsp;“bob”&nbsp;插入到&nbsp;$var&nbsp;的开头处。*/<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bob'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;下面两个例子使用&nbsp;“bob”&nbsp;替换&nbsp;$var&nbsp;中的&nbsp;“MNRPQR”。*/<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bob'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bob'</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">7</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;从&nbsp;$var&nbsp;中删除&nbsp;“MNRPQR”。*/<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

  </div>
  </p>
  <p class="para">
   <div class="example" id="example-4817">
    <p><strong>Example #2 
     Using <span class="function"><strong>substr_replace()</strong></span> to replace multiple strings at
     once
    </strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$input&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'A:&nbsp;XXX'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'B:&nbsp;XXX'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'C:&nbsp;XXX'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;A&nbsp;simple&nbsp;case:&nbsp;replace&nbsp;XXX&nbsp;in&nbsp;each&nbsp;string&nbsp;with&nbsp;YYY.<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">';&nbsp;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'YYY'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">)).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;A&nbsp;more&nbsp;complicated&nbsp;case&nbsp;where&nbsp;each&nbsp;replacement&nbsp;is&nbsp;different.<br /></span><span style="color: #0000BB">$replace&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'AAA'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'BBB'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'CCC'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">';&nbsp;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$replace</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">)).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Replace&nbsp;a&nbsp;different&nbsp;number&nbsp;of&nbsp;characters&nbsp;each&nbsp;time.<br /></span><span style="color: #0000BB">$length&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">';&nbsp;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$replace</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$length</span><span style="color: #007700">)).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
A: YYY; B: YYY; C: YYY
A: AAA; B: BBB; C: CCC
A: AAAXX; B: BBBX; C: CCC
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.substr-replace-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">此函数可安全用于二进制对象。</span></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.substr-replace-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.str-replace.html" class="function" rel="rdfs-seeAlso">str_replace()</a> - 子字符串替换</span></li>
    <li class="member"><span class="function"><a href="function.substr.html" class="function" rel="rdfs-seeAlso">substr()</a> - 返回字符串的子串</span></li>
    <li class="member"><a href="language.types.string.html#language.types.string.substr" class="link">字符串访问与修改</a></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.substr-count.html">substr_count</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.substr.html">substr</a></div>
 <div class="up"><a href="ref.strings.html">字符串函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
